const app = new BasicTableVue({
    data() {
        const header = new Map()
            .set('houseName', '所在机房')
            .set('ip', '应用IP')
            .set('port', '端口')
            .set('actualDomain', '实际域名')
            .set('visitCount', '访问次数')
            .set('actualType', '实际方式')
            .set('exceptionType', '异常类型')
            .set('regDomain', '登记域名')
            .set('regType', '登记方式')
            .set('dealStatus', '处置状态')
            .set('dealUser', '处置人')
            .set('fstFoundTime', '首次发现时间')
            .set('lastFoundTime', '最近发现时间');
        return {
            api: slogAbnormalIpApi,
            columns: [
                new TableColumn({width: 30, type: 'selection'}),
                new TableColumn({prop: 'houseName', title: '所在机房'}),
                new TableColumn({prop: 'ip', title: '应用IP'}),
                new TableColumn({prop: 'port', title: '端口',}),
                new TableColumn({prop: 'actualDomain', title: '实际域名'}),
                new TableColumn({prop: 'visitCount', title: '访问次数'}),
                new TableColumn({
                    prop: 'actualType',
                    title: '实际方式',
                    formatter: this.actualTypeFormat,
                }),
                new TableColumn({
                    prop: 'exceptionType',
                    title: '异常类型',
                    formatter: this.exceptionTypeFormat,
                }),
                new TableColumn({prop: 'regDomain', title: '登记域名'}),
                new TableColumn({
                    prop: 'regType',
                    title: '登记方式',
                    formatter: this.regTypeFormat,
                }),
                new TableColumn({
                    prop: 'dealStatus',
                    title: '处置状态',
                    formatter: this.dealStatusFormat,
                }),
                new TableColumn({
                    prop: 'reportStatus',
                    title: '上报状态',
                    formatter: this.reportStatusFormat,
                }),
                new TableColumn({prop: 'dealUser', title: '处置人'}),
                new TableColumn({prop: 'fstFoundTime', title: '首次发现时间' ,formatter: TableColumn.datetimeFormat } ),
                new TableColumn({prop: 'lastFoundTime', title: '最近发现时间' ,formatter: TableColumn.datetimeFormat }),
            ],

            /**
             * 协议列表
             */
            protocolMap:[{"id":"1","name":"TCP"},{"id":"2","name":"UDP"},{"id":"3","name":"其他"}],

            /**
             * 处置状态
             */
            dealStatusMap:[{"id":"0","name":"未处置"},{"id":"1","name":"已处置"}],
            /**
             * 上报状态
             */
            reportStatusMap: [{"id":"0","name":"未上报"},{"id":"1","name":"上报中"},{"id":"2","name":"已上报"},{"id":"3","name":"上报异常"}],
            /**
             * 异常类型
             */
            exceptionTypeMap:[{"id":"1","name":"登记保留"},{"id":"2","name":"登记有误"},{"id":"3","name":"IP未登记"}],

            /**
             *登记方式：静态，2：动态， 3：保留
             */
            regTypelist: {'1':'静态','2':'动态','3':'保留'},

            /**
             * 异常类型
             */
            exceptionTypeList:{'1':'登记保留','2':'登记有误','3':'IP未登记'},

            /**
             *实际方式：静态，2：动态， 3：保留
             */
            actualTypelist:{'1':'静态','2':'动态'},

            /**
             * 处置状态
             */
            dealStatuslist:{'0':'未处置','1':'已处置'},


            positions: [],


            /*上报信息*/
            msgInfo :'',
            /*上报数量*/
            reportNum :'',
            /*上报框是否显示*/
            isreportShow: false,

            dealData:{},
            /**
             * 批量处理显示
             */
            isTabhandleShow:false,

            /**
             * 处理结果 2屏蔽 2放行
             */
            dealResultStatus: 1,

            /**
             * 处理备注
             */
            dealmsg:'',

            /**
             * 1、处理 2、取消处理
             */
            dealStatusAction:1,
            /**
             * 控制异常数据上报模式框显示
             */
            abnormalDataUploadDialogShow: false
        }
    },
    methods: {
        createForm() {
            return new MonitorIp({
                houseId: forward.getData().id,
            })
        },
        changeLastTime(timeArray) {
            const [start, end] = timeArray || [];
            this.form.lastFoundTimeStart = start;
            this.form.lastFoundTimeEnd = end;
        },
        init() {
            this.initHouseMap();
        },
        initHouseMap: async function () {
            this.positions = await baseHouseInfoApi.selectList()
        },
        /**
         * '实际方式, 1：静态， 2：动态',
         * @param row
         * @param column
         * @returns {String|*}
         */
        actualTypeFormat: function (row, column) {
            return this.actualTypelist[row.actualType];
        },
        /**
         * 登记方式， 1：静态，2：动态,3保留
         * @param row
         * @param column
         * @returns {String|*}
         */
        regTypeFormat: function (row, column) {
            return this.regTypelist[row.regType];
        },

        /**
         * 异常类型,1: IP登记保留，实际为启用;2: IP登记域名有误3: IP 未登记
         * @param row
         * @param column
         * @returns {String|*}
         */
        exceptionTypeFormat: function (row, column) {
            return this.exceptionTypeList[row.exceptionType];
        },

        /**
         * 处置状态，0：未处置，1：已处置
         * @param row
         * @param column
         * @returns {String|*}
         */
        dealStatusFormat: function (row, column) {
            return this.dealStatuslist[row.dealStatus];
        },
        /**
         * 上报状态翻译
         * @param row
         * @param column
         * @param val
         * @param key
         * @returns {undefined}
         */
        reportStatusFormat(row, column, val, key) {
            return this.reportStatusMap[row.reportStatus].name;
        },
        reportShow(){
            var filter = this.selectedList.filter((data) => parseInt(data.reportStatus) == 0)
            this.selectedList = filter;
            if (filter.length > 0) {
                this.reportNum = filter.length + '条,已上报的已忽略';
                this.isreportShow = true;
            } else {
                rxPrompt.msg('请勾选未上报的数据');
            }
        },
        /**
         * 确认上报
         * @returns {Promise.<void>}
         */
        report: async function () {
            //TODO 接口待确认
            const ids = this.selectedList.map((obj) => obj.id);
            if (ids.length > 0) {
                const reportData = {
                    ids: ids,
                    msgInfo: this.msgInfo,
                };
                const res = await slogAbnormalIpApi.reportData(reportData);
                console.log(res);
                if (res.data) {
                    rxPrompt.msg("上报成功");
                } else {
                    rxPrompt.msg('上报失败');
                }
            }else{
                rxPrompt.msg("没有上报的数据");
            }
            this.isreportShow = false;
        },
        /**
         * 重置确认上报
         */
        resetReport: function () {
            this.selectedIdList=[];
            this.mgInfo='';
            this.selectedList = [];
            this.reportNum='没有符合规则日志';
        },


        /**
         * 确认异常处理
         */
        tabHandleSub: async function () {
            const disposedIds = this.selectedList.filter((obj) => parseInt(obj.dealStatus) !== 0).map((obj) => obj.id)
            if (disposedIds.length !== 0) {
                rxPrompt.msg('只能勾选未处置的数据');
                return;
            }
            if(!this.checkSelected()){
                return
            }
            const ids = this.selectedList.filter((obj) => parseInt(obj.dealStatus) === 0).map((obj) => obj.id)

            this.dealData.ids = ids;
            this.dealData.msgInfo = this.dealmsg;
            this.dealData.dealUser = await sysLoginApi.currentUser().loginName;
            this.dealData.dealResultStatus = this.dealResultStatus;

            const res = await slogAbnormalIpApi.handle(this.dealData)
            if (res) {
                if (res.data) {
                    await rxPrompt.msg('处置成功');
                    this.searchPage();
                    this.isTabhandleShow = false;
                    return;
                }
            }
            await rxPrompt.msg('处置失败,请稍后重试')
        },
        /**
         * 重置异常处理
         */
        resetTabhandle:  function () {
            this.multipleSelection=[];
            this.dealmsg='';
            this.dealResultStatus = 1;
        },
        /**
         * 取消处置
         */
        cancelHandle: async function () {
            const disposedIds = this.selectedList.filter(obj => parseInt(obj.dealStatus) !== 1).map(obj => obj.id);
            if (disposedIds.length !== 0) {
                await rxPrompt.msg('只能勾选已处置的数据')
                return;
            }
            if(!this.checkSelected()){
                return
            }
            const ids = this.selectedList.filter(obj => parseInt(obj.dealStatus) === 1).map(obj => obj.id);

            this.dealData.ids = ids;
            this.dealData.msgInfo = '';
            this.dealData.dealUser = await sysLoginApi.currentUser().loginName;

            const res = await slogAbnormalIpApi.cancelHandle(this.dealData);
            if (res) {
                if (res.data) {
                    await rxPrompt.msg('取消处置成功');
                    this.searchPage();
                    return;
                }
            }
            await rxPrompt.msg('取消处置失败,请稍后重试')
            this.searchPage();
        },
        /**
         * 手动上报不正常的IP
         * @returns {Promise<void>}
         */
        manualReportAbnormalIp: async function(){
            const confirmResult = await rxPrompt.confirm('确定要上报吗？', ['确认', '取消'])
            if(!confirmResult)
                return;

            if(!this.checkSelected()){
                return
            }

            const selectedData = this.selectedList.filter(obj => parseInt(obj.reportStatus) === 0).map(obj => obj);
            if(selectedData.length == 0){
                await rxPrompt.msg('请选择未上报的记录');
                return;
            }

            const res = await slogAbnormalIpApi.reportAbnormalData(selectedData);
            if (res) {
                if (res.data) {
                    await rxPrompt.msg('操作成功');
                    this.searchPage();
                }
            }
        }
    },
    watch: {
        'form.protocol': async function (newVal, oldVal) {
            this.form.protocol=newVal;
        },

    },


})
